package com.njust.bean;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.SQLException;

public class IOCTest3 {

    //带有关闭容器方法的ioc，实验十使用
    private ConfigurableApplicationContext ioc;

    @Before
    public void tearUp(){
        System.out.println("执行前");
        ioc = new ClassPathXmlApplicationContext("config/ioc3.xml");
    }

    /**
     * 实验12：引用外部属性文件★
     */
    @Test
    public void test12(){
        //1、从容器中拿到连接池
        //DataSource bean = (DataSource) ioc.getBean("dataSource");

        //2、按照类型获取组件，可以获取到这个类型下的所有实现类子类等等...
        DataSource bean = ioc.getBean(DataSource.class);

        try {
            System.out.println(bean.getConnection());
        } catch (SQLException e) {
            Assert.fail("连接数据库异常！");
        }

        Car bean2 = ioc.getBean(Car.class);
        System.out.println(bean2);
    }
}
